﻿package net.nicoptere.utils 
{
	
	/**
	 * @author nicolas barradeau
	 */
	public class StringUtil 
	{
		
		/**
		*       Determines whether the specified string begins with the spcified prefix.
		*       @param input The string that the prefix will be checked against.
		*       @param prefix The prefix that will be tested against the string.
		*       @return True if the string starts with the prefix, false if it does not.
		*/      
		public static function beginsWith(input:String, prefix:String):Boolean
		{                       
				return (prefix == input.substring(0, prefix.length));
		}       
     


		/**
		* Check if the given email address string is valid or not
		* @param    email:String    Email address string that is to be checked
		* @return    Boolean        Returns true if valid email, false otherwise
		*/
		static public function checkMail( input:String ):Boolean
		{
			
			var reg:RegExp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i;
			return reg.test( input );
			
		}
		
		/**
		*       Determines whether the specified string contains the needle
		*       @param input the string that the needle will be checked against.
		*       @param needle the string to find in the inpus String
		*       @param caseSensitive is the search case sensitive
		*       @return True if the input string contains the needle, false otherwise.
		*/      
		public static function contains(input:String, needle:String, caseSensitive:Boolean = false ):Boolean
		{          
			if ( caseSensitive )
			{
				var input:String = input.toUpperCase();
				var needle:String = needle.toUpperCase();
			}
			return input.lastIndexOf( needle ) != -1;
		}       
     
		/**
		*       Determines whether the specified string ends with the spcified suffix.
		*       @param input The string that the suffic will be checked against.
		*       @param prefix The suffic that will be tested against the string.
		*       @return True if the string ends with the suffix, false if it does not.
		*/      
		public static function endsWith(input:String, suffix:String):Boolean
		{
				return (suffix == input.substring(input.length - suffix.length));
		}  
		
		/**
		* Check if the given string contains numbers or not
		* @param    input:String    string that is to be checked
		* @return    Boolean        Returns true if the string contains numbers, false otherwise
		*/
		static public function hasNumbers( input:String ):Boolean
		{
			var reg:RegExp = /\d/;
			return reg.test( input );
		}
		
		/**
		* Check if the given string is empty or not
		* @param    input:String    string that is to be checked
		* @return    Boolean        Returns true if the string is empty, false otherwise
		*/
		public static function isEmpty( input:String ):Boolean
		{
			return ( trim(input) == '' );
		}
		
		  
		
		
		/**
		* Check if the given string is a valid mobile phone number
		* @param    input:String    string that is to be checked
		* @return    Boolean        Returns true if the string is a valid mobile phone number, false otherwise
		*/
		public static function isMobilePhone ( input:String, defaultValue:String = '' ):Boolean
		{
			if (isEmpty (input) || input == defaultValue || input.substr(0, 2) != "06" || input.length != 10)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		
		/**
		*       Removes all instances of the remove string in the input string
		*       @param input The string that will be checked for instances of remove string
		*       @param remove The string that will be removed from the input string
		*       @return A String with the remove string removed
		*/      
		public static function remove(input:String, remove:String):String
		{
			return StringUtil.replace(input, remove, "");
		}
		/**
		 * @usage removes all the spaces of the input string
		 * @param	input a string with spaces
		 * @return String a string without spaces
		 */
		public static function removespaces( input:String ):String
		{
			return input.replace( /[ ]+/g, '' );
		}
		/**
		* escapes the special characters of the string ( é -> e, à -> a )
		* @param    input:String    string that is to be checked
		* @return    String        returns the modified string
		*/
		static public function removeSpecialChars( input:String ):String
		{
			
			var _special:String = 'àâäéèêëïïöôûüùç';
			var _escape:String = 'aaaeeeeiioouuuc';
			 
			var special:Array = _special.split('');
			var escape:Array = _escape.split('');
			
			var i:int;
			for ( i = 0; i < special.length; i++ )
			{
				input = input.replace( special[ i ], escape[ i ] );
			}
			return input;
		}   
		
		/**
		*       Replaces all instances of the replace string in the input string with the replaceWith string.
		*       @param input The string that instances of replace string will be replaces with removeWith string.
		*       @param replace The string that will be replaced by instances of the replaceWith string.
		*       @param replaceWith The string that will replace instances of replace string.
		*       @return A new String with the replace string replaced with the replaceWith string.
		*/
		public static function replace(input:String, replace:String, replaceWith:String):String
		{
				return input.split(replace).join(replaceWith);
		}
		
		/**
		 * @usage trims the blank spaces before and after the input string
		 * @param	input a string with blank spaces at the beginning and at the end
		 * @return the trimmed string
		 */
		public static function trim(input:String):String
		{
			return input.replace( /^\s+/g, '' ).replace( /\s+$/g, '');
		}

		/**
		 * @usage trims the blank spaces before the input string
		 * @param	input a string with blank spaces at the beginning
		 * @return trimmed string
		 */
		public static function ltrim(string:String):String 
		{
			return string.replace( /^\s+/, '' );
		}
		
		/**
		 * @usage trims the blank spaces after the input string
		 * @param	input a string with blank spaces at the end
		 * @return	trimmed string
		 */
		public static function rtrim(input:String):String
		{
			return input.replace( /\s+$/g, '' );
		}
		

	}
}